package com.pb.infra.auth.controller;

import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.auth.service.LoginService;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 登录管理模块
 *
 * @Author:liudebao
 * @Date: 2019/7/16
 */
@Slf4j
@RestController
@RequestMapping("v1/login")
public class LoginController {

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "privi_user";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "登录模块";

    /**
     * 业务逻辑对象
     */
    @Resource
    private LoginService loginService;

    /**
     * 登录校验
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("loginCheck")
    public Result loginCheck(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
        log.info("login/loginCheck 登录校验 》》请求参数：{}", paramMap);
        Map<String, Object> responseMap = new HashMap<>();
        responseMap = loginService.loginCheck(paramMap, response);
        return Result.ok(responseMap);
    }

    /**
     * 读取某用户配置的功能权限信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @GetMapping("getUserInfo")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result getUserInfo(@RequestParam Map<String, Object> paramMap, HttpServletRequest request) {
        return loginService.getUserInfo(paramMap, request);
    }

    /**
     * 登出系统
     *
     * @param request 请求参数
     */
    @PostMapping("logout")
    public void logout(HttpServletRequest request) {
        loginService.logout(request);
    }

    /**
     * 读取登录用户能访问的菜单信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @GetMapping("getRouter")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result getRouter(@RequestParam Map<String, Object> paramMap, HttpServletRequest request) {
        return Result.ok(loginService.getRouter(paramMap, request));
    }

    /**
     * 读取职位访问菜单
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @GetMapping("getPositionRouter")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result getPositionRouter(@RequestParam Map<String, Object> paramMap, HttpServletRequest request) {
        return Result.ok(loginService.getPositionRouter(paramMap, request));
    }

    /**
     * 修改用户密码
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("editPassword")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result editPassword(@RequestBody Map<String, Object> paramMap, HttpServletRequest request) {
        loginService.updatePassword(paramMap, request);
        return Result.OK_EMPTY;
    }

    /**
     * 锁定用户
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("lockUser")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result lockUser(@RequestBody Map<String, Object> paramMap) {
        loginService.lockUser(paramMap);
        return Result.OK_EMPTY;
    }

}
